/*
 * Copyright 2013 UUGU TEAM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.uugu.controler;

import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;

import javax.annotation.Resource;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Analyzer.TokenStreamComponents;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.util.Attribute;
import org.apache.lucene.util.AttributeImpl;
import org.springframework.stereotype.Controller;
import org.uugu.voice.Speaker;

/**
 * @author Silence
 *
 */
@Controller
public class BaseAction implements Action {

	@Resource
	private Speaker speaker;
	
	SmartChineseAnalyzer analyzer;
	
	TokenStreamComponents tsc;
	
	public void action(String text){
		// TODO 语句分词
		 StringReader reader = new StringReader(text);
		 String str = "";
		 StringBuffer sb = new StringBuffer();
		 try {
//			analyzer = new SmartChineseAnalyzer(org.apache.lucene.util.Version.LUCENE_43);
//			tsc = analyzer.createComponents(null, reader);
//			TokenStream ts = null;
//			ts = tsc.getTokenStream();
//			Tokenizer tk = tsc.getTokenizer();
//			
//			Iterator<AttributeImpl> it = tk.getAttributeImplsIterator();
//			while(ts.incrementToken()){
//				System.out.println(ts);
//			}
//	            	TermAttribute termAtt = (TermAttribute) ts.addAttribute(TermAttribute.class);
//	            	str = "";
//	          
//	            if(str.length()>=2){
//	                System.out.println("##"+str);//判断等于二个字的,如果是一个字就不要了
//	            }
//	            
//	            sb.append(str);
	            
		 } catch (Exception e) {
			say("主人，分词出现了异常情况！");
			e.printStackTrace();
		 }
		 
		// TODO 关键字抽取
		
		// TODO 统计关键词权重
		
		// TODO 领域判定
		String item = "天气";
		
		// TODO 查询结果或执行动作
		
		if("天气".equals(text)){
			text = "大连明天天气晴，16-24摄氏度，是个好天气！建议主人带着女主人您出去玩一下。";
		}
		// 读取信息
		say(text);
		
	}
	
	/* (non-Javadoc)
	 * @see org.uugu.controler.Action#getItem(java.lang.String)
	 */
	@Override
	public String getItem(String arg) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 信息语音读取方法
	 * 
	 * @param String 语音的文本
	 **/
	private void say(String text){
		speaker.say(text);
	}

}
